//
//  UserViewController.m
//  LT
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UserViewController.h"
#import "UserTableViewCell.h"
#import "myCollectViewController.h"
#import "myFootmarkViewController.h"
#import "myWannatViewController.h"
#import "loginViewController.h"
#import "UIImageView+WebCache.h"

static NSString *const UserCell = @"UserCell";


@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *UserTableView;

@property(nonatomic,strong)NSMutableArray *nameArray;

@property(nonatomic,strong)NSMutableArray *imageArray;

@property(nonatomic,strong)UIButton *zhuxiaoButton;

//三个button
@property(nonatomic,strong)UIButton *tieButton;
@property(nonatomic,strong)UILabel *tieLabel;

@property(nonatomic,strong)UIButton *jinButton;
@property(nonatomic,strong)UILabel *jinLabel;

@property(nonatomic,strong)UIButton *xingButton;
@property(nonatomic,strong)UILabel *xingLabel;



@end

@implementation UserViewController

//写在方法之前跳回来之后会先执行这个方法,这时候就有名字了
//吧用户名赋值给nameLabel
//-(void)viewDidAppear:(BOOL)animated{
//
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    NSString *image = [defaults objectForKey:@"image"];
//    NSString *name = [defaults objectForKey:@"name"];
//    
//    if (name != nil) {
//        [self.headPic sd_setImageWithURL:[NSURL URLWithString:image]];
//        [self.userNameButton setTitle:name forState:UIControlStateNormal];
//   
//    }
//}


- (void)viewWillAppear:(BOOL)animated
{
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    NSString *userName = [defaults objectForKey:@"userName"];
//    NSString *url = [defaults objectForKey:@"userImage"];
    
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"loginStatus"]isEqualToString:@"1"]) {
//        
//        //    NSLog(@"111");
//       
//        NSLog(@"%@",userName);
//        NSLog(@"%@",url);
////        [self.imageButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]] forState:UIControlStateNormal];
////        [self.headPic setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]]];
////        [self.userNameButton setTitle:userName forState:UIControlStateNormal];
////        [self.userNameButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//        
//    }else{
    
//        [self.userNameButton setTitle:@"登录 / 注销" forState:UIControlStateNormal];
        
        
        
//    }
//    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //用户页背景颜色

    
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x343434);
    
    self.UserTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.UserTableView];
    self.UserTableView.delegate = self;
    self.UserTableView.dataSource = self;
    self.UserTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"btn_actionsheet"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.hidesBackButton = YES;
    self.nameArray = [NSMutableArray arrayWithObjects:@"我的收藏",@"清除缓存",nil];
    self.imageArray = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"like"],[UIImage imageNamed:@"like"],nil];
  
    
    self.UserTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x343434);

    
    //头视图
    self.backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0 * WIDTHCELL, 0 * WIDTHCELL, self.view.frame.size.width, 350 * WIDTHCELL)];
    [self.backImage setImage:[UIImage imageNamed:@"LT"]];
    
    self.backImage.userInteractionEnabled = YES;
    self.UserTableView.tableHeaderView = self.backImage;

    
    //头视图上放装帖子的label
    self.backLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,217 * WIDTHCELL, self.view.frame.size.width, 133 * WIDTHCELL)];
    self.backLabel.backgroundColor = [UIColor whiteColor];
    self.backLabel.alpha = 0.5;
    [self.backImage addSubview:self.backLabel];
   // [self getButton];
   
    //头视图上放夜间模式的button
    self.nightButton = [[UIButton alloc]initWithFrame:CGRectMake(350 * WIDTHCELL, 30 * WIDTHCELL, 30 * WIDTHCELL, 30 * WIDTHCELL)];
    self.nightButton.layer.masksToBounds = YES;
    self.nightButton.layer.cornerRadius = 15 * WIDTHCELL;
    self.nightButton.backgroundColor = [UIColor yellowColor];
    [self.UserTableView addSubview:self.nightButton];
    [self.nightButton setBackgroundImage:[UIImage imageNamed:@"night"] forState:UIControlStateNormal];
    [self.nightButton addTarget:self action:@selector(nightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //注册
    [self.UserTableView registerClass:[UserTableViewCell class] forCellReuseIdentifier:UserCell];
      
}

//夜间模式
-(void)nightButtonAction:(UIButton *)button{
   
        if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
            NSLog(@"白天");
            [self.dk_manager dawnComing];
            [self.nightButton setBackgroundImage:[UIImage imageNamed:@"day"] forState:UIControlStateNormal];
            
        } else {
            NSLog(@"晚上");
            
            [self.dk_manager nightFalling];
            
             [self.nightButton setBackgroundImage:[UIImage imageNamed:@"night"] forState:UIControlStateNormal];
        }
    

}




-(void)getButton{

//label上放3个button
    self.tieButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.tieButton setBackgroundImage:[UIImage imageNamed:@"tiezi2"] forState:UIControlStateNormal];
    self.tieButton.frame = CGRectMake(60 * WIDTHCELL, 230 * WIDTHCELL, 45 * WIDTHCELL,45 * WIDTHCELL);
    self.tieButton.backgroundColor = [UIColor clearColor];
    [self.UserTableView addSubview:self.tieButton];
    
    self.tieLabel = [[UILabel alloc]initWithFrame:CGRectMake(45 * WIDTHCELL, 285 * WIDTHCELL, 110 * WIDTHCELL, 40 * WIDTHCELL)];
    self.tieLabel.backgroundColor = [UIColor clearColor];
    self.tieLabel.text = @"收藏的帖子";
    self.tieLabel.textColor = [UIColor whiteColor];
    [self.UserTableView addSubview:self.tieLabel];
    
    
    self.jinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.jinButton setBackgroundImage:[UIImage imageNamed:@"jinnang2"] forState:UIControlStateNormal];
    self.jinButton.frame = CGRectMake(190 * WIDTHCELL, 230 * WIDTHCELL, 45 * WIDTHCELL, 45 * WIDTHCELL);
    self.jinButton.backgroundColor = [UIColor clearColor];
    [self.UserTableView addSubview:self.jinButton];
    
    self.jinLabel = [[UILabel alloc]initWithFrame:CGRectMake(180 * WIDTHCELL, 285 * WIDTHCELL, 80 * WIDTHCELL, 40 * WIDTHCELL)];
    self.jinLabel.backgroundColor = [UIColor clearColor];
    self.jinLabel.text = @"我的锦囊";
    self.jinLabel.textColor = [UIColor whiteColor];
    [self.UserTableView addSubview:self.jinLabel];
    
    self.xingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.xingButton setBackgroundImage:[UIImage imageNamed:@"xingcheng2"] forState:UIControlStateNormal];
    self.xingButton.frame = CGRectMake(320 * WIDTHCELL, 230 * WIDTHCELL, 52 * WIDTHCELL, 52 * WIDTHCELL);
    self.xingButton.backgroundColor = [UIColor clearColor];
    [self.UserTableView addSubview:self.xingButton];
    
    self.xingLabel = [[UILabel alloc]initWithFrame:CGRectMake(310 * WIDTHCELL, 285 * WIDTHCELL, 80 * WIDTHCELL, 40 * WIDTHCELL)];
    self.xingLabel.backgroundColor = [UIColor clearColor];
    self.xingLabel.text = @"我的行程";
    self.xingLabel.textColor = [UIColor whiteColor];
    [self.UserTableView addSubview:self.xingLabel];
  
}

#pragma mark cell的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //此处有一个逻辑,如果用户已经登录,那么底下的四个都可以点击,如果没有登录,要提示登录
      NSUInteger size = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = size / 1024.0 / 1024.0;
    
    //UserTableViewCell *cell = [[UserTableViewCell alloc]init];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) { //跳转到我的收藏界面
            myCollectViewController *mycollectVC = [[myCollectViewController alloc]init];
            UINavigationController *mycollectNa = [[UINavigationController alloc]initWithRootViewController:mycollectVC];
            
            [self presentViewController:mycollectNa animated:YES completion:^{
            
            }];

        }
        if (indexPath.row == 1){
            UIAlertController *alertAction = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"确定删除%.2fM缓存吗?",cacheSize] message:nil preferredStyle:UIAlertControllerStyleAlert];
            
        [alertAction addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        }]];
        [alertAction addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
          
            [[SDImageCache sharedImageCache] clearDisk];
            [[SDImageCache sharedImageCache] clearMemory];
            [self.UserTableView reloadData];
          
        }]];
            
            [self presentViewController:alertAction animated:YES completion:nil];
               }
    }
    
  }


#pragma mark 设置section数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}


#pragma mark tableView每个分区要显示的行数(cell的个数)必须实现的方法之一
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.nameArray.count;
    
}




//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 70;



}

#pragma mark 必须实现的方法之2,作用是每显示一个cell都会执行这个方法.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:UserCell];
    
    
    cell.userLabel.text = [self.nameArray objectAtIndex:indexPath.row];
    cell.imageView.image = [self.imageArray objectAtIndex:indexPath.row];
    return cell;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
